/**
 * Created by wenPeng on 2016/12/22.
 */
$(function () {
    // 判断用户是否登录
    var uuid  = Cookie.getCookie()['uuid'],
        switch1 = $('#switch1')[0],
        switch2 = $('#switch2')[0],
        switch3 = $('#switch3')[0],
        switch4 = $('#switch4')[0],
        switch5 = $('#switch5')[0],
        line = $('.line')[0],
        scroller1 = $('#scroller1')[0],
        scroller2 = $('#scroller2')[0],
        scroller3 = $('#scroller3')[0],
        scroller4 = $('#scroller4')[0],
        scroller5 = $('#scroller5')[0],
        maxScrollX = -4 * $(scroller1).width(),
        getUrl = '',
		deleteUrl = '',
        template = '',
        goTo = '',
        goInfo = '',
        setmeatType = function (obj) {
            obj.pageSize = obj.pageSize ? obj.pageSize : 10;
            obj.getUrl = obj.getUrl?obj.getUrl:getUrl;
            obj.id = obj.id?obj.id:'';
            obj.status = obj.status?obj.status:'';
            obj.dataTypes ="pageSize=" + obj.pageSize +'&id='+ obj.id + '&status='+ obj.status + '&' +Infornation;
            return obj;
        },
        requestData = function (obj,fun) {
            $.ajax({
                url: host + obj.getUrl,
                type: 'post',
                data: obj.dataTypes,
                success: function (data) {
                    fun(data)
                }
            });
        },
        deleteOk = function (dom) {
            ;$('#maskBox button').off('click').on('click',function(){
                $('#maskBox').addClass('active');
                var id = $(dom).parents('li').data('id');
                requestData(setmeatType({
                    getUrl:deleteUrl,
                    id: id
                }),function (data) {
                    if(data.success){
                        $('#maskBox').addClass('active');
                        $(dom).parents('li').fadeOut(function () {
                            $('li[data-id='+id+']').remove()
                            $(jroll1.scroller).find('.jroll-infinite-page').removeAttr('style')
                            $(jroll2.scroller).find('.jroll-infinite-page').removeAttr('style')
                            $(jroll5.scroller).find('.jroll-infinite-page').removeAttr('style')
                            jroll1.refresh();
                            jroll2.refresh();
                            jroll5.refresh();
                            lay.msg('操作已成功')
                        });
                    }else {
                        $('#maskBox').addClass('active');
                        layer.open({
                            content: '删除失败！是否刷新后重试？'
                            ,btn: ['是', '否']
                            ,shadeClose:true
                            ,yes: function(index){
                                layer.close(index);
                                window.location.reload();
                            }
                            ,no: function (index) {
                                layer.close(index);

                            }
                        });
                    }

                })
            })
            $('#maskBox1').on('click',function(){
                $('#maskBox').addClass('active');
            })
        };
    ImporInfor();
    if(!uuid) {
        layer.open({
            content: '您还没有登录，请先登录！'
            ,btn: ['去登录', '去首页']
            ,shadeClose:false
            ,yes: function(index){
                window.location.href = 'login.html';
                layer.close(index);
            }
            ,no: function (index) {
                window.location.href = 'index.html';
                layer.close(index);
            }
        });
        return;
    }
    // 区分 采购供应信息
    if(getQueryString('purchase')){
        $('#header-tab h5').text('采购消息')
        $('#footer-nav button').text('发布新采购')
        document.title = '找牛网-采购消息';
        getUrl = '/myhome/purchaseInfo/getMyPurchaseInfos';
        deleteUrl = "/myhome/purchaseInfo/deletePurchaseInfo";
        template = '<li class="clear" onclick="globalFun.goDetails(this)" data-status={{=_obj.status}} data-id={{=_obj.id}}><p>{{=(["审核中","已发布","失败","洽谈中","失效"])[_obj.status]}}</p><div class="txt"> <div class="title-time"><p>{{=_obj.meatCategory}}</p><p>{{=_obj.updateDate}}</p></div><div class="content">{{=_obj.purchaseDesc}}</div><div class="address-man"><p>{{=_obj.country}}&nbsp;(产地)</p><p>{{=_obj.adminName}}(交易员) </p></div>{{=_obj.status==4 || _obj.status==1?"<button onclick=globalFun.delet(this)>删除消息</button>":""}}</div></li>';
        goTo = 'releasePurchasingMessage.html';
        goInfo = 'wantToBuyInfo1.html';
    }else if(getQueryString('supply')){
        $('#header-tab h5').text('供应消息')
        $('#footer-nav button').text('发布新供应')
        document.title = '找牛网-供应消息';
        getUrl = '/myhome/provide/getMyProvideInfos';
        deleteUrl = '/myhome/provide/deleteProvideInfo';
        template = '<li class="clear" onclick="globalFun.goDetails(this)" data-status={{=_obj.status}} data-id={{=_obj.id}}><p>{{=(["审核中","已发布","失败","洽谈中","失效"])[_obj.status]}}</p><div class="txt"> <div class="title-time"><p>{{=_obj.meatCategory}}</p><p>{{=_obj.updateDate}}</p></div><div class="content">{{=_obj.provideDesc}}</div><div class="address-man"><p>{{=_obj.country}}&nbsp;(产地)</p><p>{{=_obj.adminName}}(交易员) </p></div>{{=_obj.status==4 || _obj.status==1?"<button onclick=globalFun.delet(this)>删除消息</button>":""}}</div></li>';
        goTo = 'releaseStockMessage.html';
        goInfo = 'deliveryDetails.html';
    }
    window.globalFun = {
        delet:function (dom) {
            $('#maskBox').removeClass('active');
            deleteOk(dom)
        },
        goDetails: function (dom) {
            var status = $(dom).data('status'),
                id = $(dom).data('id');
            if(status == 1){
                window.location.href = goInfo + '?id='+id;
            }
        }
    }
    function jroll(domId,opa,requdata) {
        var jrollinit = new JRoll(domId, opa);   // jroll 对象
        // $('#meal-info').css('overflow','visible')
        // 下拉刷新
        jrollinit.pulldown({
            spinning:false,
            refresh: function(complete) {
                requestData(setmeatType(requdata),function (data) {
                    if (data.success == true) {
                        complete();
                        jrollinit.scroller.innerHTML = "";    //清空内容
                        jrollinit.infinite_callback(data.data);
                    } else {
                        //无数据则返回什么
                        console.log(data)
                    }
                });
            }
        });
        jrollinit.left = $(jrollinit.scroller).offset().left;
        // 无限加载

        jrollinit.infinite({
            options: jrollinit,
            completeTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">已经加载完毕全部数据</div>',
            loadingTip: '<div class=\"jroll-infinite-tip\" style=\"text-align\: center\;\">正在加载...</div>',
            template: template,
            // template: '<li _dataNum ="{{=_obj.id}}"><h3>{{=_obj.meatCategory[0]?_obj.meatCategory[0]:"暂无数据"}}</h3><p><em>¥{{=_obj.price?_obj.price+ "/吨":"议定"}}</em></p><ol class="clear mian_delivery_ol"><li>数量：<span>{{=Math.round(_obj.amount / 1000 * 100) / 100}}</span>吨</li><li>交货地：<span>{{=_obj.deliveryArea}}</span></li><li class="mian_delivery_ol_add"><button class="{{=_obj.class = _obj.status === 3?\"colorName\":\"\"}}">{{=_obj.status===3?\"供应中\":\"供应\"}}</button></li></ol></li>',
            getData: function (page, callback) {
                var history = window.sessionStorage.getItem('orderBuyhistory');
                if(history){

                }else {
                    requestData(setmeatType(requdata),function (data) {
                        if (data.success == true) {
                            jrollinit.options.total = Math.ceil(data.total / 10);
                            if(page >= jrollinit.options.total){
                                jrollinit.options.page = jrollinit.options.total;
                            }
                            callback(data.data)
                        } else {
                            //无数据则返回什么
                            console.log(data)
                        }
                    });
                }
            },
        });
        return jrollinit;
    }
    // var jroll1 = new JRoll("#information", {
    //     bounce: true,
    //     scrollX:true,
    //     scroller: scroller1
    // });
    var jroll1 = jroll('#information',{
        scroller:scroller1,
        scrollX:true,
        maxX:maxScrollX,
        id:'scroller1'
    },{status : ''})
    // var jroll2 = new JRoll("#information", {
    //     bounce: true,
    //     scrollX:true,
    //     scroller: scroller2
    // });
    var jroll2 = jroll('#information',{
        scroller:scroller2,
        scrollX:true,
        maxX:maxScrollX,
        id:'scroller2'
    },{status : 1})
//     var jroll3 = new JRoll("#information", {
//         bounce: true,
//         scrollX:true,
//         scroller: scroller3
//     });
    var jroll3 = jroll('#information',{
        scroller:scroller3,
        scrollX:true,
        maxX:maxScrollX,
        id:'scroller3'
    },{status : '0'})
//     var jroll4 = new JRoll("#information", {
//         bounce: true,
//         scrollX:true,
//         scroller: scroller4
//     });
    var jroll4 = jroll('#information',{
        scroller:scroller4,
        scrollX:true,
        maxX:maxScrollX,
        id:'scroller4'
    },{status : 3})
//     var jroll5 = new JRoll("#information", {
//         bounce: true,
//         scrollX:true,
//         scroller: scroller4
//     });
    var jroll5 = jroll('#information',{
        scroller:scroller5,
        scrollX:true,
        maxX:maxScrollX,
        id:'scroller5'
    },{status : 4})
//
    function switchTo(x, lineX) {

        $(line).css({
            left:lineX+'px'
        })
        jroll1.scrollTo(x, jroll1.y, 200);
        jroll2.scrollTo(x, jroll2.y, 200);
        jroll3.scrollTo(x, jroll3.y, 200);
        jroll4.scrollTo(x, jroll4.y, 200);
        jroll5.scrollTo(x, jroll5.y, 200);
    }
    function scrolling(a, b,c,d) {
        if(this.s !== "scrollY"){
            this.options.momentum = false;
            a.scrollTo(this.x, a.y);
            b.scrollTo(this.x, b.y);
            c.scrollTo(this.x, c.y);
            d.scrollTo(this.x, d.y);
        }else {
            this.options.momentum = true;
        }
    }
// 事件
    // jroll1
    jroll1.on("scroll", function() {
        scrolling.call(this, jroll2, jroll3,  jroll4,  jroll5);
    });
    jroll1.on("scrollEnd", function() {
        if (this.x < -50) {
            switchTo(maxScrollX / 4, line.offsetWidth); // 第二页
        } else if (this.x >= -50 && this.x < 0) {
            switchTo(0, 0);                             // 第一页
        }
    });

    // jroll2
    jroll2.on("scroll", function() {
        scrolling.call(this, jroll1, jroll3,  jroll4,  jroll5);
    });
    jroll2.on("scrollEnd", function() {
        // -375  -----   -750
        if (this.x <= maxScrollX / 4 - 50) {
            switchTo(maxScrollX / 4 * 2, line.offsetWidth * 2); //第三页
        } else if (this.x >= maxScrollX / 4 + 50) {
            switchTo(0, 0);                                     //第一页
        }else {
            switchTo(maxScrollX / 4, line.offsetWidth);         //第二页
        }
    });
    // jroll3
    jroll3.on("scroll", function() {
        scrolling.call(this, jroll1, jroll2,  jroll4,  jroll5);
    });
    jroll3.on("scrollEnd", function() {
        if (this.x <= maxScrollX / 4 * 2 - 50) {
            switchTo(maxScrollX / 4 * 3, line.offsetWidth * 3); //第四页
        } else if (this.x >= maxScrollX / 4 * 2 + 50) {
            switchTo(maxScrollX / 4, line.offsetWidth);         //第二页
        }else {
            switchTo(maxScrollX / 4 * 2, line.offsetWidth * 2); //第三页
        }
    });
    // // jroll4
    jroll4.on("scroll", function() {
        scrolling.call(this, jroll1, jroll2,  jroll3, jroll5);
    });
    jroll4.on("scrollEnd", function() {
        if (this.x <= maxScrollX / 4 * 3 - 50) {
            switchTo(maxScrollX, line.offsetWidth * 4);     //第五页
        } else if (this.x >= maxScrollX / 4 * 3 + 50) {
            switchTo(maxScrollX / 4 * 2, line.offsetWidth * 2); //第三页
        }else {
            switchTo(maxScrollX / 4 * 3, line.offsetWidth * 3); //第四页
        }
    });
    // // jroll4
    jroll5.on("scroll", function() {
        scrolling.call(this, jroll1, jroll2,  jroll3,  jroll4);
    });
    jroll5.on("scrollEnd", function() {
        if (this.x >= maxScrollX + 50) {
            switchTo(maxScrollX / 4 * 3, line.offsetWidth * 3); // 第四页
        } else if (this.x >= maxScrollX - 50 && this.x < maxScrollX) {
            switchTo(maxScrollX, line.offsetWidth * 4);         // 第五页
        }else {
            switchTo(maxScrollX, line.offsetWidth * 4);         //第五页
        }
    });
    switch1.onclick = function() {
        switchTo(0, 0);                                         //第一页
    }
    switch2.onclick = function() {
        switchTo(maxScrollX / 4, line.offsetWidth);             //第二页
    }
    switch3.onclick = function() {
        switchTo(maxScrollX / 4 * 2, line.offsetWidth * 2);     //第三页
    }
    switch4.onclick = function() {
        switchTo(maxScrollX / 4 * 3, line.offsetWidth * 3);     //第四页
    }
    switch5.onclick = function() {
        switchTo(maxScrollX, line.offsetWidth * 4);             //第五页
    }

    $('#footer-nav button').on("click",function(){
        window.location.href = goTo;
    })
    //个人中心四个页面点击返回
    $('#header-tab span').on('click',function(){
        window.location.href = 'personal.html';
    })
})